/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package AlertSystem;

import java.sql.*;

/**
 *
 * @author quang
 */
public class DBConnection {
    public static Connection setConnection(String url, String userName, String password) throws Exception
  {
      Connection connection;
    
      //Register the JDBC driver for MySQL.
      Class.forName ("com.mysql.jdbc.Driver").newInstance ();

      //get a connection to the database
      connection = DriverManager.getConnection(url, userName, password);

      return connection;
  }

  public static ResultSet query(Connection connection, String query) {

    ResultSet result = null;
    try {
      PreparedStatement stmt = connection.prepareStatement(query);
      result=stmt.executeQuery();
    }
    catch (Exception ex) {
        ex.printStackTrace();
        System.out.println("exception in DB : "+ex);
//        System.exit(0);
    }
    return result;
  }

  public static void updateQuery(Connection connection, String query) {
	      Statement stmt = null;
	      try {
	        stmt = connection.createStatement();
	        stmt.executeUpdate(query);
	      }
	      catch (SQLException ex) {
                  ex.printStackTrace();
	    	  System.out.println("exception in DB : "+ex);
	      }
	    }

  public static void closeConnection(Connection connection){
    try {
      connection.close();
    }
    catch (SQLException ex) {   
        ex.printStackTrace();
    }
  }
}
